//-------------------------------------------------------------------------
// Copyright (c) 2000-2016 Digital. All Rights Reserved.
//
// This software is the confidential and proprietary information of
// Digital
//
// Original author: allnas
//
//-------------------------------------------------------------------------
// APACHE
//-------------------------------------------------------------------------
package com.zj.dubbo.user.web;

import com.zj.dubbo.entity.User;
import com.zj.dubbo.response.DubboResponse;
import com.zj.dubbo.response.Result;
import com.zj.dubbo.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * @author zhaojin
 */
@Slf4j
@RestController
@RequestMapping("user")
public class UserController {
    @Reference(group = "user-service")
    private UserService userService;

    @PostMapping("save")
    public Result<Integer> save(User user) {
        return getIntegerResult(user, userService.save(user));
    }

    private Result<Integer> getIntegerResult(User user, DubboResponse<Integer> save) {
        log.info("user:[{}]", user);
        Result<Integer> result = null;
        if (save.getCode() == HttpStatus.OK.value()) {
            result = new Result<>(HttpStatus.OK.value(), "", save.getData());
        } else {
            result = new Result<>(HttpStatus.BAD_REQUEST.value(), save.getMessage(), null);
        }
        return result;
    }

    @PostMapping("batchSave")
    public Result<Integer> batchSave(List<User> users) {
        log.info("users:[{}]", users);
        Result<Integer> result = null;
        DubboResponse<Integer> response = userService.batchSave(users);
        if (response.getCode() == HttpStatus.OK.value()) {
            result = new Result<>(HttpStatus.OK.value(), "", response.getData());
        } else {
            result = new Result<>(HttpStatus.BAD_REQUEST.value(), response.getMessage(), null);
        }
        return result;
    }

    @DeleteMapping("delete")
    public Result<Integer> delete(Integer userId) {
        log.info("userId:[{}]", userId);
        Result<Integer> result = null;
        DubboResponse<Integer> response = userService.delete(userId);
        if (response.getCode() == HttpStatus.OK.value()) {
            result = new Result<>(HttpStatus.OK.value(), "", response.getData());
        } else {
            result = new Result<>(HttpStatus.BAD_REQUEST.value(), response.getMessage(), null);
        }
        return result;
    }

    @PutMapping("update")
    public Result<Integer> update(User user) {
        return getIntegerResult(user, userService.update(user));
    }

    @PostMapping("check")
    public Result<String> check(User user) {
        log.info("user:[{}]", user);
        Result<String> result = null;
        DubboResponse<String> response = userService.check(user);
        if (Objects.equals(response.getCode(), HttpStatus.OK.value())) {
            result = new Result<>(HttpStatus.OK.value(), "", response.getData());
        } else {
            result = new Result<>(HttpStatus.BAD_REQUEST.value(), response.getMessage(), null);
        }
        return result;
    }
}
